﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace Generator
{
    class Helper
    {

        static Dictionary<string, string> s_connDict;
        public static Dictionary<string, string> ConnDict
        {
            get
            {
                if (s_connDict == null)
                {
                    s_connDict = new Dictionary<string, string>();
                    foreach (ConnectionStringSettings item in ConfigurationManager.ConnectionStrings)
                    {
                        s_connDict.Add(item.Name, item.ConnectionString);
                    }
                }
                return s_connDict;
            }
        }

        static Dictionary<string, string> dataTypeMap;
        public static Dictionary<string, string> DataTypeMap
        {
            get
            {
                if (dataTypeMap == null)
                {
                    dataTypeMap = new Dictionary<string, string>();
                    var dataTypeMaps = ConfigurationManager.AppSettings.Get("dataTypeMaps").Trim();
                    var lines = dataTypeMaps.Split(new char[] { ';' }, StringSplitOptions.None);
                    foreach (var line in lines)
                    {
                        var kv = line.Trim().Split(new string[] { "=>" }, StringSplitOptions.None);
                        dataTypeMap.Add(kv[0], kv[1]);
                    }
                }
                return dataTypeMap;
            }
        }

        public static string GetDefVal(string defval)
        {

            if (defval.IndexOf("getdate()") >= 0)
            {
                return "getdate()";
            }
            else if (defval.IndexOf("''") >= 0)
            {
                return "'''";
            }
            else
            {
                return defval.Replace("(", "").Replace(")", "");
            }
        }

    }
}
